package com.wangyazhu.MybatisDemo;

import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {
    /**
     * 根据姓名查找用户
     * @param name
     * @return
     */
    @Select("SELECT * FROM USER WHERE NAME = #{name}")
    User findByName(@Param("name") String name);


    /**
     * 根据提交的name, age插入用户
     * @param name
     * @param age
     * @return
     */
    @Insert("INSERT INTO USER(NAME, AGE) VALUES(#{name}, #{age})")
    int insert(@Param("name") String name, @Param("age") Integer age);


    /**
     * 查找所有用户并返回
     * @return
     */
    @Results({
            @Result(property = "name", column = "name"),
            @Result(property = "age", column = "age")
    })
    @Select("SELECT * FROM USER")
    List<User> findAll();

    /**
     * 根据name更新用户的age
     * @param user
     */
    @Update("UPDATE USER SET age=#{age} WHERE name=#{name}")
    void update(User user);

    /**
     * 根据用户id删除用户信息
     * @param id
     */
    @Delete("DELETE FROM USER WHERE id=#{id}")
    void delete(Long id);

}
